package com.ruoyi.web.controller.workOrder;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workOrder.domain.WorkOrder;
import com.ruoyi.workOrder.service.IWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 运维工单Controller
 * 
 * @author likui
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/workOrderManager/workOrder")
public class WorkOrderController extends BaseController {
    @Resource private IWorkOrderService workOrderService;

    /**
     * 查询运维工单列表
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder) {
        startPage();
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 导出运维工单列表
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:export')")
    @Log(title = "运维工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder) {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "运维工单数据");
    }



    @Log(title = "运维工单导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:export')")
    @PostMapping("/importWorkOrder")
    public AjaxResult importData( MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        List<WorkOrder> workOrderList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = workOrderService.importWorkOrder(workOrderList, updateSupport, operName);
        return AjaxResult.success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WorkOrder> util = new ExcelUtil<>(WorkOrder.class);
        util.importTemplateExcel(response, "工单数据");
    }

    /**
     * 获取运维工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId) {
        return AjaxResult.success(workOrderService.selectWorkOrderByWorkId(workId));
    }

    /**
     * 新增运维工单
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:add')")
    @Log(title = "运维工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder) {
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改运维工单
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:edit')")
    @Log(title = "运维工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder) {
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除运维工单
     */
    @PreAuthorize("@ss.hasPermi('workOrderManager:workOrder:remove')")
    @Log(title = "运维工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds) {
        return toAjax(workOrderService.deleteWorkOrderByWorkIds(workIds));
    }

}
